<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate" />
    <meta http-equiv="Pragma" content="no-cache" />
    <meta http-equiv="Expires" content="0" />
    <link rel="stylesheet" type="text/css" href="style.css" /> 
    <title>Wifi Sound System Config</title>
</head>

<body>
    <div class="credential_form">
    <?php //phpinfo();
    include "global.php";
    
    //load XML file
    $path = config_credential_path();
    $known_credential_list = load_xml_from_file($path);
    $wlan_scan = shell_exec('/usr/bin/sudo /sbin/iwlist scan | grep ESSID');
    $array_essid_raw = explode("\n", $wlan_scan);
    //extract avaiable ESSID to make a combo list
    
    ?>
    <table>
    <tr>
        <th class="known_essid">ESSID</th>
        <th class="separator">:</th>
        <th class="known_pass">Password</th>
    </tr>
    <!-- input form to save new network -->
    <tr><form action="submit.php" method="post">
        <td><select name="save_essid" id="save_essid" dir="rtl">
        <?php
        foreach($array_essid_raw as $item){
            if (strlen($item) > 0){
                $tmp = explode(":", $item);
                $tmp = str_replace("\"", "", $tmp[1]);
                if (strlen($tmp) > 0){
                    echo "
                    <option value=\"", $tmp, "\">", $tmp, "</option>";
                }
            }
        }
        ?>
        </select></td>
        <td class="separator">:</td>
        <td><input type="text" name="save_password" id="save_password"></td>
        <td><input type="submit" value="save" class="credential_button"></td>
    </form></tr>
    
    <?php
    //list of known networks
    foreach($known_credential_list as $cred){
    ?>
    <tr>
        <td class="known_essid"><?php echo $cred->ssid ?></td>
        <td class="separator">:</td><td class="known_pass"><?php echo $cred->pass ?></td>
        <td>
            <form action="submit.php" method="post">
                <input type="submit" value="forget" class="credential_button">
                <input type="hidden" name="forget_essid" value="<?php echo $cred->ssid ?>"> 
            </form>
        </td>
    </tr>
    <?php
    }
    ?>
    </table>
    </div>
</body>
</html>
